package com.baozun.houji.order.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.KeyEvent
import android.view.View
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentTransaction
import com.alibaba.android.arouter.facade.annotation.Route
import com.baozhun.mall.common.arouter.RoutePath
import com.baozhun.mall.common.base.BaseActivity
import com.baozhun.mall.common.ext.showToast
import com.baozhun.mall.common.util.StatusBarUtil
import com.baozun.houji.order.R
import com.baozun.houji.order.databinding.ActivityOrderSearchBinding
import com.baozun.houji.order.fragment.OrderListFragment
import com.baozhun.mall.common.model.enums.OrderStatus
import com.baozun.houji.order.viewmodel.OrderListViewModel

/**
 * @Title：订单搜索页面
 * @Description：
 * @Author：xuefeng.zhu
 * @Since：2022/11/4
 * @Version：1.0.0
 */
@Route(path = RoutePath.Order.ORDER_SEARCH)
class OrderSearchActivity : BaseActivity<OrderListViewModel, ActivityOrderSearchBinding>(), View.OnKeyListener {

    private val mOrderListFragment by lazy { OrderListFragment.newInstance(orderStatus = OrderStatus.ALL, isSearchOrder = true) }

    companion object {
        fun start(context: Context) {
            var intent = Intent(context, OrderSearchActivity::class.java)
            context.startActivity(intent)
        }
    }

    override fun initView(savedInstanceState: Bundle?) {
        StatusBarUtil.setColor(this, getColor(android.R.color.transparent))
        StatusBarUtil.setTransparentForImageView(this, mDataBinding.titleBar)
        mDataBinding.etOrderSearch.setOnKeyListener(this)
        val manager: FragmentManager = supportFragmentManager
        val transaction: FragmentTransaction = manager.beginTransaction()
        transaction.replace(R.id.container, mOrderListFragment)
        transaction.commitAllowingStateLoss()
    }

    override fun createObserver() {

    }

    override fun onKey(v: View?, keyCode: Int, event: KeyEvent?): Boolean {
        return if (keyCode == KeyEvent.KEYCODE_ENTER && event?.action == KeyEvent.ACTION_UP) {
            val keywords: String = mDataBinding.etOrderSearch.text.toString()
            if (keywords.isNotBlank()) {
                mOrderListFragment?.searchOrder(keywords)
            } else {
                showToast(com.baozhun.mall.res.R.string.search_key_empty_hint)
            }
            true
        } else {
            false
        }
    }
}